
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package project2;

import java.util.*;

/**
 *
 * @author Tasse
 */
public class Sequence<E> {

    protected Object[] data;
    protected int size = 0; // the number of elements in the Sequence, not the
                            // capacity of the data array

    /**
     * Initializes this Sequence object to be empty, with an initial capacity of
     * ten elements.
     *
     */
    public Sequence() {
        data = new Object[10];
    }
            
            
    /**
     * Initializes this Sequence object to be empty, with a specified initial
     * capacity.
     *
     * @param capacity – the initial capacity of this Sequence object.
     *
     * @throw IllegalArgumentException – if capacity is non-positive.
     *
     */
    public Sequence(int n) {
        if (n < 1) {
            throw new IllegalArgumentException();
        }
        
        data = new Object[n];
    }
            
            
     /**
     * Returns the number of elements in this Sequence object.
     *
     * @return the number of elements in this Sequence object.
     *
     */
    public int size() {
        return size;
    }
            
                        
    /**
     * Appends a specified element to this Sequence object.
     *     
     * @param element – the element to be inserted at the end of this Sequence
     * object.
     *     
     */
    public void append(E element) {
        if (size < data.length) { //If there is room in the array, just add it
        data[size]=element;
        size++;
        
        } else { //If there is not enough room, copy the array to a temporary twice the size, then call it data.
            
            Object[] tempData = Arrays.copyOf(data, data.length * 2);
            data = tempData;
            
            data[size]=element;
            size++;
            
        }
    }
            
            
    /**
     * Returns the element at a specified index in this Sequence object. The
     * worstTime(n) is constant, where n is the number of elements in this
     * Sequence object.
     *     
     * @param k – the index of the element returned.
     *     
     * @return the element at index k in this Sequence object.
     *     
     * @throws IndexOutOfBoundsException – if k is either negative or greater
     * than or equal to the number of elements in this Sequence Sequence object.
     *     
     */
    public E get(int k) {
        if (k < 0 || k > size) {
            throw new IndexOutOfBoundsException();
        }
        
        return (E) data[k]; //This is probably not what is supposed to be done, but this was the only way to get it to compile...
    }
            
            
    /**
     * Changes the element at a specified index in this Sequence object. The
     * worstTime(n) is constant, where n is the number of elements in this
     * Sequence object.
     *     
     * @param k – the index of the element returned.
     * @param newElement – the element to replace the element at index k in this
     * Sequence object.
     *     
     * @throws IndexOutOfBoundsException – if k is either negative or greater
     * than or equal to the number of elements in this Sequence object.
     *     
     */
    public void set(int k, E newElement) {
        if (k < 0 || k >= size) {
            throw new IndexOutOfBoundsException();
        }
        
        data[k] = newElement;
    }
        
        















}
